/**
 * Created by dale on 2017/8/16.
 */
$(document).ready(function () {
    
    
    var form = new xhForm($('#xh_form'));

    form.parse();
    var iDepId=addressUri.getParam('iDepId');
    if(iDepId!=null){
        $.ajax({
            url:moduleUrl+'department/getDepInfo',
            data:{id:iDepId},
            type:'POST',
            dataType:'json',
            success:function (data) {
                form.fill(data);
                $('#cc').combotree('setValue', data['iParentId']);
                form.bindRequired($('#bt_modal_confirm',parent.document));
            },
            error:function (data) {
                console.log(data);
            }
        });


        $("#cc").combotree({

            onBeforeSelect : function(node) {
                var nowNode = $('#cc').combotree("tree").tree('find', iDepId);
                if(node['id']==iDepId){  //如果是自己
                    return false;
                }
                var flag=false;
                $.each($("#cc").combotree("tree").tree('getChildren', nowNode.target),function (index,obj) {
                    if(obj['id']==node['id']){
                        flag=true;
                        return false;
                    }
                });
                if(flag){
                    return false;
                }
            }
        });
    }else {
        var iParentId=addressUri.getParam('iParentId');
        if(iParentId!=null){
            $('#cc').combotree('setValue', iParentId);
        }
        form.bindRequired($('#bt_modal_confirm',parent.document));
    }


    $('#bt_modal_confirm',parent.document).click(function () {
        $.ajax({
            url:moduleUrl+'department/saveDepartment',
            data:$('#xh_form').serialize(),
            type:'POST',
            success:function (data) {
                // window.top.swal(data['responseText'],'','success');
                window.top.xhTop.getActiveFrame().depList.treegrid('reload');
                window.top.xhTop.closeModal();
            },
            error:function (data) {
                window.top.swal(data['responseText'],'','error');
                // console.log(data);
            }
        });

    });

    modalBind.bindClick();

    

});